package ch.ffhs.contacts;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;

/**
 * class contact. a contact contains information like name and number of an
 * emergency-contact
 * 
 * @author mf
 */
public class Contact {
	public final static String CONTACT_NO_NUMBER = "-- keine Nummer --";
	public final static String CONTACT_NOT_DEFINED = "-- nicht definiert --";

	private String name;
	private String number;
	private Boolean checked;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * returns a phone-number or error-text
	 * 
	 * @return phone-number of contact or Error-Text CONTACT_NO_NUMBER
	 */
	public String getNumber() {
		return number;
	}

	/**
	 * returns a phone-number for sending sms without error-text
	 * 
	 * @return phone-number of contact or 0000 if no valid number is found
	 */
	public String getNumberSMS() {
		if (number != CONTACT_NO_NUMBER) {
			return number;
		} else {
			return "0000";
		}
	}

	public void setNumber(String number) {
		this.number = number;
	}

	/**
	 * indicates, if a message was already sent to this number
	 * 
	 * @return true, if message was sent
	 */
	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public String toString() {
		return name + " -- " + number;
	}

	/**
	 * creates a new contact by loading information from preferences by given
	 * key
	 * 
	 * @param context
	 *            context for getting sharedPreferences
	 * @param key
	 *            identifier for sharedPreferences
	 */
	public Contact(Context context, String key) {
		this.number = CONTACT_NO_NUMBER;
		this.name = CONTACT_NOT_DEFINED;
		this.checked = false;
		loadContact(context, key);
	}

	private void loadContact(Context context, String key) {
		// load contact from preferences
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		String uriString = prefs.getString(key, "");

		if (!uriString.isEmpty()) {
			Uri uri = Uri.parse(uriString);
			loadContactText(context, uri);
		}
	}

	private void loadContactText(Context context, Uri uri) {
		Cursor cursor = context.getContentResolver().query(uri, null, null,
				null, null);
		while (cursor.moveToNext()) {
			this.name = cursor
					.getString(cursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
			String hasPhone = cursor
					.getString(cursor
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER));
			if ((Integer.parseInt(hasPhone) == 1)) {
				// get phone-number
				loadPhoneNumber(context, uri);
			}
		}
		cursor.close();
	}

	private void loadPhoneNumber(Context context, Uri uri) {
		Cursor phones = context.getContentResolver().query(uri, null, null,
				null, null);
		while (phones.moveToNext()) {
			int phoneType = phones
					.getInt(phones
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
			if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
				this.number = phones
						.getString(phones
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
				this.number = this.number.replaceAll("\\s", "");
				break;
			}
		}
		phones.close();
	}
}
